﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.VCCodeModel;
using System.Collections;
using Microsoft.VisualStudio.Shell.Interop;

namespace GordonKristan.codeclean
{
	class InsertModule : TextEditModule
	{
		public const uint INSERT_LINE_AFTER_METHOD = 0x0001;

		/// <summary>
		/// InsertModule constructor.
		/// </summary>
		/// <param name="aDoc">String for module to style</param>
		public InsertModule(String aDoc) : base(aDoc)
		{

		}

		/// <summary>
		/// Performs addition of all whitespace
		/// </summary>
		/// <param name="flags">Option flags from settings</param>
		private void insertWhitespace(uint flags)
		{
			if ((flags | INSERT_LINE_AFTER_METHOD) > 0)
			{
				insertLineAfterMethod();
			}
		}

		/// <summary>
		/// Adds a line after all method definitions
		/// </summary>
		private void insertLineAfterMethod()
		{
			//Grab the current document code model
			DTE dte = (DTE)Package.GetGlobalService(typeof(DTE));
			VCFileCodeModel file = (VCFileCodeModel)dte.ActiveDocument.ProjectItem.FileCodeModel;
			//Create an array to hold all method endpoints
			ArrayList points = new ArrayList();
			//Go through each function in the file
			foreach (VCCodeFunction f in file.Functions)
			{
				//Ensure that we only deal with functions in THIS file
				if (String.Compare(f.File, dte.ActiveDocument.FullName, true) == 0)
				{
					points.Add(f.EndPoint);
				}
			}
			//Sort the elements in the array. Last points first.
			IComparer sorter = (IComparer)new TextPointSorter();
			points.Sort(sorter);
			//Go through each point, and ensure there's a blank line
		}

		/// <summary>
		/// Gets the remove style property from the options page.
		/// </summary>
		/// <returns>Remove style flags</returns>
		private uint getStyleFlags()
		{
			uint ret = 0x0;
			//Get the environment
			DTE env = (DTE)Package.GetGlobalService(typeof(SDTE));
			if (env == null)
			{
				return 0x0;
			}
			//Get the prosperities in the environment
			EnvDTE.Properties props = env.get_Properties("CodeClean", "Style");
			//Go through each value and get the ones we want.
			foreach (Property p in props)
			{
				if (p.Name == "InsertLineAfterMethod" && (bool)p.Value)
				{
					ret |= INSERT_LINE_AFTER_METHOD;
				}
			}
			return ret;
		}

	}
}
